package uit.edu.vn.Utility;

import java.util.ArrayList;
import java.util.List;

import uit.edu.vn.Activity.R;
import uit.edu.vn.Model.myMusic;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;


public class MusicListAdapter extends BaseAdapter{
	private Context context;
	private List<myMusic> listMusic ;
	private ArrayList<Boolean> itemChecked = new ArrayList<Boolean>();

	public MusicListAdapter(Context context, List<myMusic> listMusic) 
	{
	// TODO Auto-generated constructor stub
		
		this.context=context;
		this.listMusic=listMusic;
		// thiêt lap mac dinh itemcheck là false
		for (int i = 0; i < this.getCount(); i++) {
		       itemChecked.add(i, false); // initializes all items value with false
		   }
	}
	@Override
	public int getCount() 
	{
		// TODO Auto-generated method stub
		return listMusic.size();
	}
	@Override
	public Object getItem(int position) 
	{
		// TODO Auto-generated method stub
		return listMusic.get(position);	
		}
	@Override
	public long getItemId(int position) 
	{
		// TODO Auto-generated method stub
		return position;
	}
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		// TODO Auto-generated method stub
		myMusic entry= listMusic.get(position);
		if(convertView==null)
		{
			LayoutInflater inflate =(LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			convertView=inflate.inflate(R.layout.row_informusic, null);
		}
		TextView txt_MusicName= (TextView)convertView.findViewById(R.id.txt_MusicName);
	    TextView txt_MusicArtist= (TextView)convertView.findViewById(R.id.txt_MusicArtist);
	    // dua data vao entry
	    txt_MusicArtist.setText(entry.getArist());
	    txt_MusicName.setText(entry.getName());
	    //xy lu check box 
		 CheckBox cbox= (CheckBox)convertView.findViewById(R.id.chkSelectMusic);
		 cbox.setOnClickListener(new OnClickListener() {

		        public void onClick(View v) {

		            CheckBox cb = (CheckBox) v.findViewById(R.id.chkSelectMusic);

		            if (cb.isChecked()) {
		                itemChecked.set(position, true);
	
		            } else if (!cb.isChecked()) {
		                itemChecked.set(position, false);
		
		            }
		        }
		    });

		cbox.setChecked(itemChecked.get(position));
		return convertView;
	}
	// hàm lấy setcheck va get check de lay itemChecklist
	public ArrayList<Boolean> getCheckList()
	{
		return itemChecked;
	}
	public ArrayList<Boolean> setCheckList(ArrayList<Boolean> itemCheck)
	{
		itemChecked=itemCheck;
		return itemChecked;
	}
}

